﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web.Mvc;
using AutoMapper;
using GeneralUtilities.Utilities.Unity;
using IdeaDomain.DomainLayer;
using IdeaDomain.DomainLayer.Entities;
using IdeaDomain.ServiceLayer;
using Microsoft.Practices.EnterpriseLibrary.PolicyInjection;
using NoteTableUI.Models;
using NoteTableUI.Models.Translators;
using UtilityComponent.Filters;


namespace NoteTableUI.Controllers
{
    [CheckinLogin]
    public class IdeaController : Controller
    {
        private IIdeaDataService IdeaDataService { get; set; }
        private IIdeaService IdeaService { get; set; }

        public IdeaController(IIdeaService ideaService, IIdeaDataService rowService)
        {
            IdeaService = ideaService;
            IdeaDataService = rowService;
        }

        public IdeaController()
        {
            IdeaService = PolicyInjection.Wrap<IIdeaService>(Container.Resolve<IdeaService>());
            IdeaDataService = PolicyInjection.Wrap<IIdeaDataService>(Container.Resolve<IIdeaDataService>());
        }

        [AutoMapperConfigurationActionFilter(typeof(IdeaDomainNoteTableProfile))]
        public ActionResult Index()
        {
            var userId = Convert.ToInt32(Session["UserId"]);
            var ideaList = IdeaService.GetIdeasByUser(userId);
            var modelList = Mapper.Map<List<IdeaModel>>(ideaList);
            return View(modelList);
        }

        [AutoMapperConfigurationActionFilter(typeof(IdeaDomainNoteTableProfile))]
        public ActionResult Details(int id)
        {
            var model = Mapper.Map<IdeaModel>(IdeaService.GetIdeaById(id));
            return View(model);
        }

        [AutoMapperConfigurationActionFilter(typeof(IdeaDomainNoteTableProfile))]
        public ActionResult Create()
        {
            //ToChange
            var userId = Convert.ToInt32(Session["UserId"]);
            IEnumerable<IdeaModel> ideaList = Mapper.Map<List<IdeaModel>>(IdeaService.GetIdeasByUser(userId));
            var slist = new SelectList(ideaList, "IdeaId", "IdeaName");
            ViewData["ideaList"] = slist;

            return View();
        }

        [HttpPost]
        [AutoMapperConfigurationActionFilter(typeof(IdeaDomainNoteTableProfile))]
        public ActionResult Create(IdeaModel model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var userId = Convert.ToInt32(Session["UserId"]);
                    model.UserId = userId;
                    var idea = Mapper.Map<Idea>(model);
                    IdeaService.CreateIdea(idea);
                }
                else
                {
                    ModelState.AddModelError("", Resources.Resources.IdeaController_EditColumn_The_Idea_Name_or_Column_Name_provided_is_incorrect_);
                }

                return RedirectToAction("Index", "Idea");

            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex);
                return View();
            }
        }

        [AutoMapperConfigurationActionFilter(typeof(IdeaDomainNoteTableProfile))]
        public ActionResult Edit(int id)
        {
            //ToChange
            var userId = Convert.ToInt32(Session["UserId"]);
            var idea = IdeaService.GetIdeaById(id);
            var model = Mapper.Map<IdeaModel>(idea);

            //Get all Ideas by userid to get a reference idea list.
            IEnumerable<IdeaModel> ideaList = Mapper.Map<List<IdeaModel>>(IdeaService.GetIdeasByUser(userId));
            var slist = new SelectList(ideaList, "IdeaId", "IdeaName");
            ViewData["ideaList"] = slist;

            return View(model);
        }

        [HttpPost]
        [AutoMapperConfigurationActionFilter(typeof(IdeaDomainNoteTableProfile))]
        public ActionResult Edit(IdeaModel model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var userId = Convert.ToInt32(Session["UserId"]);
                    model.UserId = userId;
                    var idea = Mapper.Map<Idea>(model);
                    var result = IdeaService.UpdateIdea(idea);
                    if (!result)
                    {
                        ModelState.AddModelError("", Resources.Resources.IdeaController_Edit_Can_Not_Update);
                    }
                }
                else
                {
                    ModelState.AddModelError("", Resources.Resources.IdeaController_EditColumn_The_Idea_Name_or_Column_Name_provided_is_incorrect_);
                }

                return RedirectToAction("Index", "Idea");
            }
            catch
            {
                return View();
            }
        }

        public ActionResult Delete(int id)
        {
            try
            {
                var result = IdeaService.DeleteIdeaById(id);
                if (!result)
                {
                    ModelState.AddModelError("", Resources.Resources.IdeaController_DeleteColumn_Can_Not_Delete);
                }
                return RedirectToAction("Index", "Idea");
            }
            catch
            {
                return RedirectToAction("Index", "Idea");
            }
        }

        [AutoMapperConfigurationActionFilter(typeof(IdeaDomainNoteTableProfile))]
        public ActionResult EditColumn(int id)
        {
            try
            {
                var userId = Convert.ToInt32(Session["UserId"]);
                var column = IdeaService.GetColumnById(id);
                var model = Mapper.Map<ColumnInIdeaModel>(column);
                //ToChange
                IEnumerable<IdeaModel> ideaList = Mapper.Map<List<IdeaModel>>(IdeaService.GetIdeasByUser(userId));
                var slist = new SelectList(ideaList, "IdeaId", "IdeaName");
                ViewData["ideaList"] = slist;

                return View(model);
            }
            catch
            {
                return View();
            }
        }

        [HttpPost]
        [AutoMapperConfigurationActionFilter(typeof(IdeaDomainNoteTableProfile))]
        public ActionResult EditColumn(ColumnInIdeaModel model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var column = Mapper.Map<ColumnInIdea>(model);
                    IdeaService.UpdateColumn(column);
                }
                else
                {
                    ModelState.AddModelError("EditColumn", Resources.Resources.IdeaController_EditColumn_The_Idea_Name_or_Column_Name_provided_is_incorrect_);
                }

                return RedirectToAction("Edit", new { Id = model.IdeaId });
            }
            catch
            {
                return RedirectToAction("Edit", new { Id = model.IdeaId });
            }
        }

        [AutoMapperConfigurationActionFilter(typeof(IdeaDomainNoteTableProfile))]
        public ActionResult DeleteColumn(int columnId, int ideaId)
        {
            try
            {
                bool result = IdeaService.DeleteColumnById(columnId);
                if (!result)
                {
                    ModelState.AddModelError("", Resources.Resources.IdeaController_DeleteColumn_Can_Not_Delete);
                }
                return RedirectToAction("Edit", new { Id = ideaId });
            }
            catch
            {
                return RedirectToAction("Edit", new { Id = ideaId });
            }
        }


    }
}
